import { ref, computed } from 'vue'
import { defineStore } from 'pinia'
import { alert_list, alert_delete, alert_add, alertdata_list, alertdata_listdelete, alertdata_listadd, evaluate_list, evaluate_listdelete, alert_edit, alertdata_listsearch } from '../utils/api'


export const alertliststore = defineStore('counterzsq', () => {

  var alertlist = ref([])
  var alertlistdata = ref([])
  var evaluatelist = ref([])
  var item = ''

  // 保存数据获取到通知类型列表数据
  function request_list() {
    alert_list().then((res) => {
      alertlist.value = res.data.alertlist
    })
  }

  // 删除通知列表数据
  function alertdelete(row) {
    alert_delete({ id: row.id }).then((res) => {
      if (res.status == 200) {
        alertlist.value = res.data.alertlist
      }
      let i = alertlist.value.findIndex((item)=>{
        return item.id = id
      })
      alertlist.value.splice(i,1)
    })
  }

  // 添加通知列表数据
  function alertadd(row) {
    alert_add(row).then((res) => {
      if (res.status == 200) {
        alertlist.value = res.data.alertlist
      }
    })
  }

  // 通知修改列表数据
  function alertedit(row) {
    alert_edit(row).then((res) => {
      if (res.status == 200) {
        console.log(res.data.alertlist);
        alertlist.value = res.data.alertlist
      }
    })
  }



  // 保存数据获取到通知类型列表数据
  function request_alertdata() {
    alertdata_list().then((res) => {
      alertlistdata.value = res.data.list
    })
  }

  // 删除通知列表数据
  function request_alertdelete(row) {
    console.log(row.id);
    alertdata_listdelete({ id: row.id }).then((res) => {
      alertlistdata.value = res.data.list
      // alertlistdata.value = localStorage.getItem('list')
      console.log(alertlistdata);
    })
  }

  // alertdata_listsearch
  // 查询通知列表数据
  function request_alertsearch(row) {
    alertdata_listsearch({ region: row }).then((res) => {
      if (res.status == 200) {
        alertlistdata.value = res.data.list
      }
    })
  }

  // 添加通知列表数据
  function request_alertadd(row) {
    console.log(row);
    alertdata_listadd(row).then((res) => {
      console.log(res);
      if (res.status == 200) {
        alertlistdata.value = res.data.list
      }
    })
  }

  // 保存数据获取到评价列表数据
  function request_evaluatelist() {
    evaluate_list().then((res) => {
      evaluatelist.value = res.data.evallist;
      console.log(res.data.evallist);
      console.log(evaluatelist);
    })
  }

  // 删除评价列表数据
  function evaluatedelete(row) {
    evaluate_listdelete({ id: row.id }).then((res) => {
      // console.log(res);
      if (res.status == 200) {
        evaluatelist.value = res.data.evallist
        // alertlist.value = localStorage.getItem('alertlist')
      }
    })
  }

  return { alertlist, evaluatelist, alertlistdata, item, request_list, alertdelete, alertadd, request_alertdata, request_alertdelete, request_alertadd, request_evaluatelist, evaluatedelete, alertedit, request_alertsearch }
})



